/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.ad.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.collect.MapUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.common.shiro.realm.LoginInfo;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.ad.entity.AdAgent;
import com.jeesite.modules.ad.service.AdAgentService;
import com.jeesite.modules.sys.utils.UserUtils;

/**
 * 广告代理商Controller
 * 
 * @author Liu,Wenzhao
 * @version 2019-04-15
 */
@Controller
@RequestMapping(value = "${adminPath}/ad/adAgent")
public class AdAgentController extends BaseController {

	@Autowired
	private AdAgentService adAgentService;

	@RequestMapping({ "checkAccount" })
	@ResponseBody
	public String checkAccount(String oldAccount, String account) {
		AdAgent a;
		(a = new AdAgent()).setAccount(account);
		if (account != null && account.equals(oldAccount)) {
			return "true";
		} else {
			return account != null && this.adAgentService.getByAccount(a) == null ? "true" : "false";
		}
	}

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public AdAgent get(String id, boolean isNewRecord) {
		AdAgent a = adAgentService.get(id, isNewRecord);
		if (a != null && StringUtils.isNotBlank(a.getUpperAgent())) {
			AdAgent pAgent = adAgentService.get(a.getUpperAgent());
			pAgent.setAccount(StringUtils.getTreeNodeName("2", pAgent.getEmail(), pAgent.getAccount()));
			a.setpAgent(pAgent);
		}
		return a;
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("ad:adAgent:view")
	@RequestMapping(value = { "list", "" })
	public String list(AdAgent adAgent, Model model) {
		AdAgent a = this.adAgentService.getCurAgent();
		adAgent.setUpperAgent(a == null ? "" : a.getId());
		model.addAttribute("adAgent", adAgent);
		if (a != null) {
			model.addAttribute("isAgent", 1);
		} else {
			model.addAttribute("isAgent", 0);
		}
		return "modules/ad/adAgentList";
	}

	@RequiresPermissions("ad:adAgent:edit")
	@RequestMapping({ "adAgentSelect" })
	public String adAgentSelect(AdAgent adAgent, String selectData, Model model) {
		String a;
		if (JsonMapper.fromJson(a = EncodeUtils.decodeUrl(selectData), Map.class) != null) {
			model.addAttribute("selectData", a);
		}

		model.addAttribute("adAgent", adAgent);
		return "modules/ad/adAgentSelect";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ad:adAgent:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<AdAgent> listData(AdAgent adAgent, HttpServletRequest request, HttpServletResponse response) {
		adAgent.setPage(new Page<>(request, response));
		Page<AdAgent> page = adAgentService.findPage(adAgent);
		return page;
	}

	/**
	 * 查看
	 */
	@RequiresPermissions("ad:adAgent:view")
	@RequestMapping(value = "detail")
	public String detail(AdAgent adAgent, Model model) {
		model.addAttribute("adAgent", adAgent);

		LoginInfo loginInfo = UserUtils.getLoginInfo();
		AdAgent curAgent = this.adAgentService.getByUserCode(loginInfo.getId());
		if (curAgent != null) {
			model.addAttribute("isAgent", 1);
		} else {
			model.addAttribute("isAgent", 0);
		}

		return "modules/ad/adAgentDetail";
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ad:adAgent:edit")
	@RequestMapping(value = "form")
	public String form(AdAgent adAgent, Model model) {
		model.addAttribute("adAgent", adAgent);

		LoginInfo loginInfo = UserUtils.getLoginInfo();
		AdAgent curAgent = this.adAgentService.getByUserCode(loginInfo.getId());
		if (curAgent != null) {
			model.addAttribute("isAgent", 1);
		} else {
			model.addAttribute("isAgent", 0);
		}

		return "modules/ad/adAgentForm";
	}

	/**
	 * 保存广告代理商
	 */
	@RequiresPermissions("ad:adAgent:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated AdAgent adAgent) {
		if (adAgent.getpAgent() != null && StringUtils.isNotBlank(adAgent.getpAgent().getId())) {
			adAgent.setUpperAgent(adAgent.getpAgent().getId());
		}
		adAgentService.save(adAgent);
		return renderResult(Global.TRUE, text("保存广告代理商成功！"));
	}

	/**
	 * 停用广告代理商
	 */
	@RequiresPermissions("ad:adAgent:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(AdAgent adAgent) {
		adAgent.setStatus(AdAgent.STATUS_DISABLE);
		adAgentService.updateStatus(adAgent);
		return renderResult(Global.TRUE, text("停用广告代理商成功"));
	}

	/**
	 * 启用广告代理商
	 */
	@RequiresPermissions("ad:adAgent:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(AdAgent adAgent) {
		adAgent.setStatus(AdAgent.STATUS_NORMAL);
		adAgentService.updateStatus(adAgent);
		return renderResult(Global.TRUE, text("启用广告代理商成功"));
	}

	/**
	 * 删除广告代理商
	 */
	@RequiresPermissions("ad:adAgent:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(AdAgent adAgent) {
		adAgentService.delete(adAgent);
		return renderResult(Global.TRUE, text("删除广告代理商成功！"));
	}

	@RequiresPermissions("ad:adAgent:edit")
	@RequestMapping(value = "treeData")
	@ResponseBody
	public List<Map<String, Object>> treeData(String curId, String upperId) {
		List<Map<String, Object>> mapList = ListUtils.newArrayList();
		AdAgent where = new AdAgent();
		where.setStatus(AdAgent.STATUS_NORMAL);
		if (StringUtils.isNotBlank(upperId)) {
			where.setUpperAgent(upperId);
			AdAgent agent = this.adAgentService.get(upperId);
			Map<String, Object> map = MapUtils.newHashMap();
			map.put("id", agent.getId());
			map.put("pId", StringUtils.defaultIfBlank(agent.getUpperAgent(), "-1"));
			map.put("name", StringUtils.getTreeNodeName("2", agent.getEmail(), agent.getAccount()));
			mapList.add(map);
		}

		List<AdAgent> list = this.adAgentService.findList(where);

		for (AdAgent agent : list) {
			if (curId != null && curId.equals(agent.getId())) {
				continue;
			}
			Map<String, Object> map = MapUtils.newHashMap();
			map.put("id", agent.getId());
			map.put("pId", StringUtils.defaultIfBlank(agent.getUpperAgent(), "-1"));
			map.put("name", StringUtils.getTreeNodeName("2", agent.getEmail(), agent.getAccount()));
//			map.put("title", agent.getAccount());
			// map.put("isParent", true);
			mapList.add(map);
		}
		return mapList;
	}

}